package com.sskj.trafficcontrol.dao;

import com.sskj.trafficcontrol.model.pojo.TcNoticeInfo;

import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

/**
 * @author zhao kuo
 * @date 2019-02-04
 */

@Mapper
@Repository
public interface TcNoticeInfoDAO {
    int deleteByPrimaryKey(Integer id);

    void insertSelective(TcNoticeInfo record);

    void insertBatch(List<TcNoticeInfo> records);

    List<TcNoticeInfo> selectByPrimaryKey(TcNoticeInfo record);

    List<TcNoticeInfo> selectByTime(TcNoticeInfo record);


    int updateByPrimaryKeySelective(TcNoticeInfo record);

    int updateByPrimaryKey(TcNoticeInfo record);


    @Select("SELECT id, open_id, notice_time, count, create_time, \n" +
            "        modify_time, form_id from tc_notice_info where form_id is null AND open_id =#{open_id} limit 1")
    TcNoticeInfo selectFormIdIsNull(String open_id);

    @Select("SELECT\n" +
            "\tid,\n" +
            "\topen_id,\n" +
            "\tnotice_time,\n" +
            "\tcount,\n" +
            "\tcreate_time,\n" +
            "\tmodify_time,\n" +
            "\tform_id," +
            "\tnotice_type \n" +
            "FROM\n" +
            "\ttc_notice_info \n" +
            "WHERE\n" +
            "\tform_id IS NOT NULL \n" +
            "\tAND open_id = #{open_id} \n" +
            "ORDER BY\n" +
            "\tnotice_time desc\n" +
            "\tlimit 1 ")
    TcNoticeInfo selectFormIdIsExists(String open_id);

    @Update("update tc_notice_info  set form_id =  #{formId},notice_city=#{noticeCity} where open_id = #{openId} and form_id is NULL ")
    void updateNotice(TcNoticeInfo tcNoticeInfo);

    @Select("SELECT count(*) FROM tc_notice_info where form_id is not null AND count = 1 AND open_id =#{open_id}")
    Integer selectTotalCount(String open_id);

    @Update("update tc_notice_info  set push_status =  #{is_open} where open_id = #{open_id} ")
    void updateNoticeStatus(@Param("open_id") String open_id,@Param("is_open") String is_open);

    @Update("update tc_notice_info  set count =  0 where open_id = #{openId} and form_id =  #{formId} ")
    void updateNoticeCount(TcNoticeInfo info);

    @Update("update tc_notice_info  set notice_time=#{noticeTime},modify_time = #{modifyTime},notice_type = #{noticeType} where open_id = #{openId} and count =  1 and notice_time=#{conditionTime}")
    void updateNotices(TcNoticeInfo tcNoticeInfo);

    @Select("SELECT * FROM tc_notice_info where  count = 1 AND open_id =#{open_id} order by notice_time DESC LIMIT 1")
    TcNoticeInfo zuijinshijian(String openId);

    @Update("update tc_notice_info  set notice_time=#{noticeTime},notice_type = #{noticeType} where open_id = #{openId} and form_id=#{formId}")
    void updateNoticeform(TcNoticeInfo tcNoticeInfo);
}